package com.lam.mall.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.context.TokenUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.mall.common.constant.MallConstant;
import com.lam.mall.entity.ProdTag;
import com.lam.mall.param.ProdTagParam;
import com.lam.mall.service.ProdTagService;

/**
 * 商品分组Controller
 * 
 * @author admin
 * @date 2023-04-20
 */
@Log(title = "商品分组")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/mall/prodTag")
public class ProdTagController extends BaseController {

    @Autowired
    private ProdTagService prodTagService;

    /**
     * 查询商品分组列表
     */
    @Log(oper="分页查询")
    @DictAutoTransfer
    @PreAuthorize("@ss.hasPm('mall:prodTag:list')")
    @GetMapping("/list")
    public Result list(ProdTagParam param) {
    
    	param.setShopId(TokenUtils.getLoginUserShopId());
        startPage();
        List<ProdTag> list = prodTagService.selectList(param);
        return Result.ok(getTableData(list));
    }

    /**
     * 获取商品分组详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('mall:prodTag:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(prodTagService.selectById(id));
    }

    /**
     * 新增商品分组
     */
    @PreAuthorize("@ss.hasPm('mall:prodTag:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody ProdTag prodTag) {
    
    	prodTag.setShopId(TokenUtils.getLoginUserShopId());
    	
        ValidateUtil.validUnique(prodTag, ValidOperType.ADD, MallConstant.UNIQUE_PROD_TAG_TITLE, "分组标题已存在！");
        
        prodTag.setIsDefault(0);
        prodTag.setProdCount(0L);
        prodTag.setCreateTime(new Date());
        prodTag.setUpdateTime(new Date());
        prodTag.setShopId(TokenUtils.getLoginUserShopId());
        
        int n = prodTagService.insert(prodTag);
    	if(n>0) {
    		prodTagService.removeProdTagCahe();
    	}
        return toAjax(n);
    }

    /**
     * 修改商品分组
     */
    @PreAuthorize("@ss.hasPm('mall:prodTag:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody ProdTag prodTag) {
    
    	prodTag.setShopId(TokenUtils.getLoginUserShopId());
    	
        ValidateUtil.validUnique(prodTag, ValidOperType.UPDATE, MallConstant.UNIQUE_PROD_TAG_TITLE, "分组标题已存在！");
        int n = prodTagService.update(prodTag);
    	if(n>0) {
    		prodTagService.removeProdTagCahe();
    	}
        return toAjax(n);
    }

    /**
     * 删除商品分组
     */
    @PreAuthorize("@ss.hasPm('mall:prodTag:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
    	int n = prodTagService.deleteById(id);
    	if(n>0) {
    		prodTagService.removeProdTagCahe();
    	}
        return toAjax(n);
    }

}
